package com.ruoyi.wechat.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.wechat.common.ServiceException;
import com.ruoyi.wechat.domain.AppUser;
import com.ruoyi.wechat.domain.SessionInfo;
import com.ruoyi.wechat.mapper.AppUserMapper;
import com.ruoyi.wechat.mapper.UserAddrMapper;
import com.ruoyi.wechat.service.IAppUserService;
import com.ruoyi.wechat.utils.LoginHelper;
import com.ruoyi.wechat.utils.WechatHelper;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

/**
 * 微信用户Service业务层处理
 *
 * @author YinHeng
 * @date 2024-01-14
 */
@Service
public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> implements IAppUserService {
    @Autowired
    private AppUserMapper appUserMapper;

    @Autowired
    private UserAddrMapper addrMapper;


    @Override
    public AppUser getByOpenid(String openid) {
        //到数据库查询用户
        LambdaQueryWrapper<AppUser> query = new LambdaQueryWrapper<>();
        query.eq(AppUser::getOpenid, openid);
        AppUser appUser = this.getOne(query);
        return appUser;
    }

    @Override
    public AppUser getByPhone(String phone) {
        //到数据库查询用户
        LambdaQueryWrapper<AppUser> query = new LambdaQueryWrapper<>();
        query.eq(AppUser::getPhone, phone);
        AppUser appUser = this.getOne(query);
        return appUser;
    }

    @Override
    public AppUser bindPhone(String entryData, String iv) {
        SessionInfo sessionInfo = LoginHelper.getSession();
        String openid = sessionInfo.getAppUser().getOpenid();
        if (ObjectUtils.isEmpty(openid)) {
            throw new ServiceException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "非小程序用户不可绑定");
        }
        AppUser curUser = this.getByOpenid(openid);
        //解密手机号
        String sessionKey = LoginHelper.getSessionKey();
        String decrypt = WechatHelper.decrypt(sessionKey, entryData, iv);
        JSONObject json = JSON.parseObject(decrypt);
        String phone = json.getString("phoneNumber");

        //查看手机号是否已经通过其他途径注册
        AppUser phUser = getByPhone(phone);
        if (!ObjectUtils.isEmpty(phUser)) {
            phUser.setOpenid(openid);
            this.updateById(phUser);
            LoginHelper.refreshSession(phUser);
            return phUser;
        }
        //手机号已经有账号，则将openid绑定到已存在的手机账号
        curUser.setPhone(phone);
        this.updateById(curUser);
        LoginHelper.refreshSession(curUser);
        return curUser;
    }

    @Override
    public void updateLocation(String lat, String lng, String area) {
        AppUser appUser = LoginHelper.getSession().getAppUser();
        AppUser dbUser = this.getById(appUser.getId());
        dbUser.setLat(lat);
        dbUser.setLng(lng);
        dbUser.setArea(area);
        this.updateById(dbUser);
    }

    @Override
    public void update(AppUser req) {
        LoginHelper.checkAuth();
        AppUser appUser = LoginHelper.getSession().getAppUser();
        AppUser dbUser = this.getByPhone(appUser.getPhone());
        if (!ObjectUtils.isEmpty(req.getNickname())) {
            dbUser.setNickname(req.getNickname());
        }
        if (!ObjectUtils.isEmpty(req.getAvatar())) {
            dbUser.setAvatar(req.getAvatar());
        }
        this.updateById(dbUser);
    }

}
